package com.qj.sys.controller;

import com.qj.bean.Result;
import com.qj.log.LogTrace;
import com.qj.page.PageResponse;
import com.qj.sys.convert.PageSysUserConvert;
import com.qj.sys.entity.dto.PageSysUserDto;
import com.qj.sys.entity.dto.SysUserDto;
import com.qj.sys.entity.po.SysUserPo;
import com.qj.sys.entity.req.PageSysUserReq;
import com.qj.sys.service.SysUserService;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * (SysUser)表控制层
 *
 * @author makejava
 * @since 2025-05-15 11:04:25
 */
@RestController
@RequestMapping("sysUser")
public class SysUserController {
    /**
     * 服务对象
     */
    @Resource
    private SysUserService sysUserService;

    /**
     * 分页查询
     *
     * @param pageSysUserReq 筛选条件
     * @return 查询结果
     */
    @GetMapping("/getPage")
    public Result<PageResponse<SysUserPo>> queryByPage(@RequestBody PageSysUserReq pageSysUserReq) {
        PageSysUserDto pageSysUserDto = PageSysUserConvert.INSTANCE.convertReqToDto(pageSysUserReq);
        return Result.ok(this.sysUserService.queryByPage(pageSysUserDto));
    }

    /**
     * 通过主键查询单条数据
     * Cacheable 添加缓存
     */
    @GetMapping("/get/{id}")
    @Cacheable(cacheNames = "SysUser", key = "'querySysUserById'+#id")
    public Result queryById(@PathVariable("id") Long id) {
        return Result.ok(this.sysUserService.queryById(id));
    }

    /**
     * 新增数据
     *
     * @param sysUserDto 实体
     * @return 新增结果
     */
    @PostMapping("/add")
    public Result add(@RequestBody SysUserDto sysUserDto) {
        return Result.ok(this.sysUserService.insert(sysUserDto));
    }

    /**
     * 编辑数据
     *
     * @param sysUserDto 实体
     * @return 编辑结果
     * CacheEvict 清理缓存
     */
    @PutMapping("/edit")
    @CacheEvict(cacheNames = "SysUser", key = "'querySysUserById'+#id")
    public Result edit(@RequestBody SysUserDto sysUserDto) {
        return Result.ok(this.sysUserService.update(sysUserDto));
    }

    /**
     * 删除数据
     *
     * @param id 主键
     * @return 删除是否成功
     */
    @DeleteMapping("/delete/{id}")
    public Result deleteById(Integer id) {
        return Result.ok(this.sysUserService.deleteById(id));
    }

    /**
     * 删除数据
     *
     * @param id 主键
     * @return 删除是否成功
     */
    @DeleteMapping("/logicDelete/{id}")
    public Result logicDeleteById(@PathVariable Long id) {
        return Result.ok(this.sysUserService.logicDeleteById(id));
    }
}

